#include <iostream>
#include <random>
#include <vector>
#include <algorithm>

// RANSAC算法中的迭代次数计算函数
int calculateIterations(int numSamples, int numInliers, double inlierProbability) {
    // 计算模型中的内点比例
    double inlierRatio = static_cast<double>(numInliers) / numSamples;
    
    // 计算迭代次数
    double logProbability = log(1 - inlierProbability);
    double logRatio = log(1 - pow(inlierRatio, numSamples));
    int iterations = logProbability / logRatio;
    
    return iterations;
}

// RANSAC算法中的距离阈值计算函数
double calculateDistanceThreshold(const std::vector<double>& distances, double outlierPercentage) {
    // 将距离按升序排序
    std::vector<double> sortedDistances = distances;
    std::sort(sortedDistances.begin(), sortedDistances.end());
    
    // 计算距离阈值索引
    int thresholdIndex = static_cast<int>(distances.size() * (1 - outlierPercentage));
    
    // 获取距离阈值
    double distanceThreshold = sortedDistances[thresholdIndex];
    
    return distanceThreshold;
}

int main() {
    // 示例使用
    int numSamples = 100; // 数据集大小
    int numInliers = 80; // 内点数目
    double inlierProbability = 0.8; // 内点的概率
    int iterations = calculateIterations(numSamples, numInliers, inlierProbability);
    
    std::cout << "Iterations: " << iterations << std::endl;
    
    std::vector<double> distances = {1.2, 0.8, 1.5, 0.6, 1.0, 0.9, 2.1}; // 距离集合
    double outlierPercentage = 0.2; // 外点的百分比
    double distanceThreshold = calculateDistanceThreshold(distances, outlierPercentage);
    
    std::cout << "Distance Threshold: " << distanceThreshold << std::endl;
    
    return 0;
}
